package com.demo2do.fms.admin.web.controller.supplier.capacity;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.AbnormalCapacitySearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.VehicleLockService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.vehicle.source.VehicleSource;
import com.demo2do.fms.entity.vehicle.source.VehicleSourceLockRecord;

/**
 * 只查询所有锁定了的记录，显示最新的一条(不包含冻结)
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/abnormal-capacity-declare")
@MenuSetting("menu-supplier")
public class AbnormalCapacityDeclareController {
    
    private static final Logger logger = LoggerFactory.getLogger(AbnormalCapacityDeclareController.class);
    
    @Autowired
    private VehicleLockService vehicleLockService;

    /**
     * 
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-abnormal-capacity-declare-management")
    public ModelAndView list(@Secure Login login,
                             HttpServletRequest request, 
                             Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity-declare/abnormal-capacity-declare-list");

        int totals = vehicleLockService.countLockRecord(login);
        if (totals > 0) {

            List<VehicleSourceLockRecord> vehicleSourceLockRecords = vehicleLockService.listLockRecord(login, page.with(totals));
            
            modelAndView.addObject("vehicleSourceLockRecords", vehicleSourceLockRecords);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }

    /**
     * search 异常运力锁定记录
     * 
     *      - 非供方， 可筛选供方
     *      - 供方， 不可筛选供方
     *      
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-abnormal-capacity-declare-management")
    public ModelAndView search(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity-declare/abnormal-capacity-declare-search");
        
        modelAndView.addObject("supplier", login.isSupplier());
        
        return modelAndView;
        
    }

    /**
     * onSearch
     * 
     * @param login
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-abnormal-capacity-declare-management")
    public ModelAndView onSearch(@Secure Login login,
                                 AbnormalCapacitySearch search,
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity-declare/abnormal-capacity-declare-list");
        modelAndView.addObject("status", search.getStatus());
        
        int totals = vehicleLockService.countLockRecord(search, login);
        if (totals > 0) {
            
            List<VehicleSourceLockRecord> vehicleSourceLockRecords = vehicleLockService.searchLockRecord(search, login, page.with(totals));
            
            modelAndView.addObject("vehicleSourceLockRecords", vehicleSourceLockRecords);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
            
        }

        return modelAndView;

    }

    /**
     * 解锁异常运力
     * 
     * @param lockRecordId
     * @return
     */
    @RequestMapping(value = "/{id}/unlock", method = RequestMethod.GET)
    @MenuSetting("submenu-abnormal-capacity-declare-management")
    public ModelAndView unlock(@PathVariable("id") Long lockRecordId) {

        ModelAndView modelAndView = new ModelAndView("supplier/capacity/capacity-declare/abnormal-capacity-declare-unlock");
        
        VehicleSourceLockRecord vehicleSourceLockRecord = vehicleLockService.loadLockRecord(lockRecordId);
        modelAndView.addObject("vehicleSourceLockRecord", vehicleSourceLockRecord);

        return modelAndView;
        
    }
    
    /**
     * 解锁异常运力
     * 
     * @param lockRecordId
     * @param operator
     * @param vehicleSource
     * @return
     */
    @RequestMapping(value = "/{lockRecordId}/unlock", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onUnlock(@PathVariable("lockRecordId") Long lockRecordId,
                                             @Preloading("vehicleSourceId") VehicleSource vehicleSource,
                                             @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        try {
            
            vehicleLockService.unlockVehicle(vehicleSource, operator, "异常运力管理手动解锁");
            
            httpResult.success().message("解锁成功");
            
        } catch (Exception e) {
            logger.error("onUnlock - unlock fail, exception is [{}]", e);
            httpResult.fail("500").message("解锁失败! " + e.getMessage());
        }

        return httpResult;
        
    }
    
}
